// pages/shopManager/goodsManager/goodsManager/goodsManager.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsType: null,
        goodsStatus: 1,
        goodsList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            goodsType: options.goodsType
        })
        this.getGoodsList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            goodsList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getGoodsList()
    },

    getGoodsList: function () {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let goodsList = this.data.goodsList
        let data
        if (this.data.goodsType == 'normal') {
            data = {
                is_sale: this.data.goodsStatus,
                is_goods: '0',
                limit: 10,
                page: this.data.nowPage
            }
        }
        else if (this.data.goodsType == 'new'){
            data = {
                is_sale: this.data.goodsStatus,
                new_goods: '1',
                limit: 10,
                page: this.data.nowPage
            }
        }
        else if (this.data.goodsType == 'share') {
            data = {
                is_sale: this.data.goodsStatus,
                share_goods: '1',
                limit: 10,
                page: this.data.nowPage
            }
        }
        else if (this.data.goodsType == 'point') {
            data = {
                store_id: wx.getStorageSync('storeId'),
                is_sale: this.data.goodsStatus,
                project_goods: '1',
                limit: 10,
                page: this.data.nowPage
            }
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Goods/goods_list', data).then(function (res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    goodsList: goodsList,
                    isAll: true
                })
            } else {
                goodsList = goodsList.concat(res)
                that.setData({
                    goodsList: goodsList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function (e) { })
    },

    navClick: function (e) {
        if (this.data.goodsStatus == e.currentTarget.dataset.status) {
            return
        }
        this.setData({
            goodsStatus: e.currentTarget.dataset.status,
            goodsList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        this.getGoodsList()
    },

    goBatchManager: function (e) {
        wx.navigateTo({
            url: '/pages/shopManager/goodsManager/batchManager/batchManager?goodsStatus=' + e.currentTarget.dataset.status + '&goodsType=' + this.data.goodsType
        })
    },

    goAddGoods: function () {
        wx.navigateTo({
            url: '/pages/shopManager/goodsManager/goodsEdit/goodsEdit?goodsType=' + this.data.goodsType,
        })
    },

    goodsEdit: function (e) {
        wx.navigateTo({
            url: '/pages/shopManager/goodsManager/goodsEdit/goodsEdit?goodsType=' + this.data.goodsType + '&goodsId=' + e.currentTarget.dataset.id,
        })
    },

    updateData: function () {
        const that = this
        let data
        if (this.data.goodsType == 'normal') {
            data = {
                is_sale: this.data.goodsStatus,
                is_goods: '0',
                limit: this.data.nowPage * 10,
                page: 0
            }
        }
        else if(this.data.goodsType == 'new'){
            data = {
                is_sale: this.data.goodsStatus,
                new_goods: '1',
                limit: this.data.nowPage * 10,
                page: 0
            }
        }
        else if (this.data.goodsType == 'share') {
            data = {
                is_sale: this.data.goodsStatus,
                share_goods: '1',
                limit: this.data.nowPage * 10,
                page: 0
            }
        }
        else if (this.data.goodsType == 'point') {
            data = {
                store_id: wx.getStorageSync('storeId'),
                is_sale: this.data.goodsStatus,
                project_goods: '1',
                limit: this.data.nowPage * 10,
                page: 0
            }
        }
        wx.showLoading({
            title: '加载中',
        })
        common.post('Goods/goods_list', data).then(function (res) {
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    goodsList: []
                })
            } else {
                that.setData({
                    goodsList: res
                })
            }
        }).catch(function (e) { })
    }
})